//
//  SHMyTableCell.m
//  UI_CarHome
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHMyTableCell.h"
#import "SHHandle.h"
@implementation SHMyTableCell
- (void)dealloc
{
    [_myLabel release];
    [_detaLabel release];
    [_isNightSwitch release];
    [_indexPath release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.myLabel = [[UILabel alloc] init];
        self.myLabel.userInteractionEnabled = YES;
        [self.contentView addSubview:self.myLabel];
        [_myLabel release];
        
        self.detaLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.detaLabel];
        self.detaLabel.textAlignment = NSTextAlignmentCenter;
        [_detaLabel release];
        self.detaLabel.text = @"   >";
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myLabel.frame = CGRectMake(10 * WID, 0, 80 * WID, 44 * WID);
    self.detaLabel.frame = CGRectMake(WIDTHS - 100 * WID, 0, 100 * WID, 44 * WID);
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    if (_indexPath != indexPath) {
        [_indexPath release];
        _indexPath = [indexPath retain];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        [self.detaLabel removeFromSuperview];
        if (self.isNightSwitch) {
            [self.isNightSwitch removeFromSuperview];
        }
        self.isNightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTHS - 100 * WID, 10, 100 * WID, 40)];
        [self.contentView addSubview:self.isNightSwitch];
      
        [self.isNightSwitch setOn:[SHSingleton shareSingletion].isNight ? YES : NO];
        [_isNightSwitch release];
        [self.isNightSwitch addTarget:self action:@selector(isNightSwitch:) forControlEvents:UIControlEventValueChanged];
    }
}
- (void)isNightSwitch:(UISwitch *)isNightSwitch {
    NSUserDefaults *isDayDefault = [NSUserDefaults standardUserDefaults];
    if (isNightSwitch.on) {
        [isDayDefault setInteger:YES forKey:@"isNight"];
        [SHSingleton shareSingletion].isNight = YES;
    } else {
        
        [isDayDefault setInteger:NO forKey:@"isNight"];
        [SHSingleton shareSingletion].isNight = NO;
    }
//    键值观察者,监控属性的值的变化,只要变化就会触发相应的操作
    [[NSNotificationCenter defaultCenter] postNotificationName:@"isNight" object:nil];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
